﻿using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Nintex.Workflow;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Workflow.ComponentModel;

namespace NintexX.Actions.InvokeNintexXAction
{
    public class InvokeNintexXActionActivity : Nintex.Workflow.Activities.ProgressTrackingActivity
    {
        public static DependencyProperty __ListItemProperty = DependencyProperty.Register("__ListItem", typeof(SPItemKey), typeof(InvokeNintexXActionActivity));
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(InvokeNintexXActionActivity));
        public static DependencyProperty __ListIdProperty = DependencyProperty.Register("__ListId", typeof(string), typeof(InvokeNintexXActionActivity));
        public static DependencyProperty WebProperty = DependencyProperty.Register("Web", typeof(string), typeof(InvokeNintexXActionActivity));
        

        public static DependencyProperty TargetAssemblyProperty = DependencyProperty.Register("TargetAssembly", typeof(string), typeof(InvokeNintexXActionActivity));
        public static DependencyProperty TargetTypeProperty = DependencyProperty.Register("TargetType", typeof(string), typeof(InvokeNintexXActionActivity));
        public static DependencyProperty InputParamProperty = DependencyProperty.Register("InputParam", typeof(string), typeof(InvokeNintexXActionActivity));

        public WorkflowContext __Context
        {
            get { return (WorkflowContext) base.GetValue(__ContextProperty);}
            set { base.SetValue(__ContextProperty, value);}
        }

        public SPItemKey __ListItem
        {
            get { return (SPItemKey)base.GetValue(__ListItemProperty); }
            set { base.SetValue(__ListItemProperty, value); }
        }

        public string __ListId
        {
            get { return (string)base.GetValue(__ListIdProperty); }
            set { base.SetValue(__ListIdProperty, value); }
        }

        public string Web
        {
            get { return (string)base.GetValue(WebProperty); }
            set { base.SetValue(WebProperty, value); }
        }

     
        public string TargetAssembly
        {
            get { return (string)base.GetValue(TargetAssemblyProperty); }
            set { base.SetValue(TargetAssemblyProperty, value); }
        }

        public string TargetType
        {
            get { return (string)base.GetValue(TargetTypeProperty); }
            set { base.SetValue(TargetTypeProperty, value); }
        }

        public string InputParam
        {
            get { return (string)base.GetValue(InputParamProperty); }
            set { base.SetValue(InputParamProperty, value); }
        }

        public InvokeNintexXActionActivity()
        {

        }

        NintexXAction action = null;
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            //Standard Nintex code to obtain context.
            ActivityActivationReference.IsAllowed(this, __Context.Web);
            NWWorkflowContext ctx = NWWorkflowContext.GetContext(
               this.__Context,
               new Guid(this.__ListId),
               this.__ListItem.Id,
               this.WorkflowInstanceId,
               this);
            
            base.LogProgressStart(ctx);

            action = (NintexXAction)Activator.CreateInstance(TargetAssembly, TargetType).Unwrap();
            action.NintexWorkFlowContex = ctx;
            action.Execute(ctx.Variables,InputParam);

           
            base.LogProgressEnd(ctx, executionContext);
            return ActivityExecutionStatus.Closed;
        }

      

        protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
        {
            Nintex.Workflow.Diagnostics.ActivityErrorHandler.HandleFault(executionContext, exception,
                this.WorkflowInstanceId, "Error while executing NintexX Action.", __ListItem.Id, __ListId, __Context);
             
            return base.HandleFault(executionContext, exception);
        }
    }
}
